﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.YiDongYuan;
using YiSha.Model.Param.YiDongYuan;
using YiSha.Service.YiDongYuan;
using YiSha.Service.OrganizationManage;
using YiSha.Entity.OrganizationManage;
using YiSha.Model.Param.OrganizationManage;

namespace YiSha.Business.YiDongYuan
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-16 08:31
    /// 描 述：机械备案申报业务类
    /// </summary>
    public class MechanicsApplyBLL
    {
        private MechanicsApplyService mechanicsApplyService = new MechanicsApplyService();
        private DetectionMechanicsService mechanicsService = new DetectionMechanicsService();
        private UserService userService = new UserService();

        #region 获取数据

        public async Task<TData<List<RegionReportEntity>>> GetRegionListByDictType(string region)
        {
            TData<List<RegionReportEntity>> obj = new TData<List<RegionReportEntity>>();
            obj.Data = await mechanicsApplyService.GetRegionListByDictType(region);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<RegionReportEntity>>> GetRegionList(string type)
        {
            TData<List<RegionReportEntity>> obj = new TData<List<RegionReportEntity>>();
            obj.Data = await mechanicsApplyService.GetRegionList(type);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<MechanicsApplyEntity>>> GetList(MechanicsApplyListParam param)
        {
            TData<List<MechanicsApplyEntity>> obj = new TData<List<MechanicsApplyEntity>>();
            obj.Data = await mechanicsApplyService.GetList(param);
            await BindHuanBaoCode(obj);
            await BindAuditUserName(obj);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<MechanicsApplyEntity>>> GetPageList(MechanicsApplyListParam param, Pagination pagination)
        {
            TData<List<MechanicsApplyEntity>> obj = new TData<List<MechanicsApplyEntity>>();
            obj.Data = await mechanicsApplyService.GetPageList(param, pagination);
            await BindHuanBaoCode(obj);
            await BindAuditUserName(obj);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        private async Task BindAuditUserName(TData<List<MechanicsApplyEntity>> obj)
        {
            var userList = await userService.GetList(new UserListParam() { UserIds = string.Join(",", obj.Data.Where(t => t.AuditUserId != 0).Select(p => p.AuditUserId).ToArray()) });
            foreach (var item in obj.Data)
            {
                if (item.AuditUserId > 0)
                {
                    item.AuditUserName = userList.Find(t => t.Id == item.AuditUserId).RealName;
                }
                else
                {
                    item.AuditUserName = string.Empty;
                }
            }
        }

        public async Task<TData<MechanicsApplyEntity>> GetEntity(long id)
        {
            TData<MechanicsApplyEntity> obj = new TData<MechanicsApplyEntity>();
            obj.Data = await mechanicsApplyService.GetEntity(id);
            if (obj.Data != null)
            {
                var entity = userService.GetEntity(obj.Data.AuditUserId.Value).Result;
                if (entity != null)
                {
                    obj.Data.AuditUserName = entity.RealName;
                }
                obj.Tag = 1;
            }
            return obj;
        }

        public async Task<TData<List<MechanicsApplyEntity>>> GetListByUserId(long userId)
        {
            TData<List<MechanicsApplyEntity>> obj = new TData<List<MechanicsApplyEntity>>();
            obj.Data = await mechanicsApplyService.GetListByUserId(userId);
            await BindHuanBaoCode(obj);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        private async Task BindHuanBaoCode(TData<List<MechanicsApplyEntity>> obj)
        {
            if (obj.Data.Where(t => t.MechanicsId != 0).Count() < 1)
            {
                return;
            }
            var mechanicsList = await mechanicsService.GetList(new DetectionMechanicsListParam { Ids = string.Join(",", obj.Data.Where(t => t.MechanicsId != 0).Select(p => p.MechanicsId).ToArray()) });
            foreach (var item in obj.Data)
            {
                if (item.MechanicsId > 0)
                {
                    item.HuanBaoCode = mechanicsList.Where(p => p.Id == item.MechanicsId).Select(p => p.HuanBaoCode).FirstOrDefault();
                }
                else
                {
                    item.HuanBaoCode = string.Empty;
                }
            }
        }

        #endregion 获取数据

        #region 提交数据

        public async Task<TData<string>> SaveForm(MechanicsApplyEntity entity)
        {
            TData<string> obj = new TData<string>();
            await mechanicsApplyService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await mechanicsApplyService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<string>> SaveAudit(string id, int auditStatus, string checkDesc, string code, long auditUser, long? detectionUser)
        {
            TData<string> obj = new TData<string>();
            await mechanicsApplyService.SaveAudit(id, auditStatus, checkDesc, code, auditUser, detectionUser);
            obj.Data = id;
            obj.Tag = 1;
            return obj;
        }

        #endregion 提交数据
    }
}
